using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TVDesigner.GUI.Elements;
using System.Text.RegularExpressions;
using TVDesigner.Generator.GeneratorEntities;

namespace TVDesigner.GUI.Controls
{
    public partial class CodePanel : UserControl
    {

        private string[] keywords = 
        {
            "abstract", "continue", "for", "new", "switch",
            "default", "goto", "package", "synchronized",
            "boolean", "do", "if", "private", "this",
            "break", "double", "implements", "protected", "throw",
            "byte", "else", "import", "public", "throws",
            "case", "instanceof", "return", "transient",
            "catch", "extends", "int", "short","try",
            "char", "final", "interface", "static", "void",
            "class", "finally", "long", "volatile",
            "const", "float", "native", "super", "while",
            "true", "false"
        };

        private FMain _MainForm;

        public FMain MainForm
        {
            get { return _MainForm; }
            set { _MainForm = value; }
        }


        public CodePanel()
        {
            InitializeComponent();
        }

        private void _RichTextBox_VisibleChanged(object sender, EventArgs e)
        {
            if (!Visible) return;

            string package = _MainForm.Project.Package;
            string xletName = _MainForm.Project.NameStartedWithUpperCase;

            TVDesigner.Entities.Elements.Screen screen = _MainForm.FocusDiagram.Screen;

            ScreenGenerator sg = new ScreenGenerator(screen);
            string code = sg.DeclareClass(package, xletName);

            _RichTextBox.Text = string.Empty;
            _Parse(code);
        }

        private void _Parse(string code)
        {
            Regex r = new Regex("\\n");
            String[] lines = r.Split(code);
            foreach (string l in lines)
            {
                _ParseLine(l);
            }
        }

        void _ParseLine(string line)
        {
            Regex r = new Regex("([ \\t{}();])");
            string[] tokens = r.Split(line);
            string selectedText;

            foreach (string token in tokens)
            {
                selectedText = null;
                // Set the token's default color and font.
                _RichTextBox.SelectionColor = Color.Black;
                _RichTextBox.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);

                // Check for a comment.
                if (token == "//" || token.StartsWith("//"))
                {
                    // Find the start of the comment and then extract the whole comment.
                    int index = line.IndexOf("//");
                    string comment = line.Substring(index, line.Length - index);
                    _RichTextBox.SelectionColor = Color.Green;
                    _RichTextBox.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);
                    selectedText = comment;
                    break;
                }

                for (int i = 0; i < keywords.Length; i++)
                {

                    if (token.IndexOf("this.") >= 0)
                    {
                        // Apply alternative color and font to highlight keyword.
                        _RichTextBox.SelectionColor = Color.Purple;
                        _RichTextBox.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
                        _RichTextBox.SelectedText = "this";

                        _RichTextBox.SelectionColor = Color.Black;
                        _RichTextBox.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);
                        selectedText = token.Substring(token.IndexOf("this") + 4);
                        break;
                    }

                    if (token.IndexOf("super.") >= 0)
                    {
                        // Apply alternative color and font to highlight keyword.
                        _RichTextBox.SelectionColor = Color.Purple;
                        _RichTextBox.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
                        _RichTextBox.SelectedText = "super";

                        _RichTextBox.SelectionColor = Color.Black;
                        _RichTextBox.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);
                        selectedText = token.Substring(token.IndexOf("super") + 5);
                        break;
                    }

                    if (keywords[i] == token)
                    {
                        // Apply alternative color and font to highlight keyword.
                        _RichTextBox.SelectionColor = Color.Purple;
                        _RichTextBox.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
                        break;
                    }
                }
                _RichTextBox.SelectedText = (selectedText != null) ? selectedText : token;
            }
            _RichTextBox.SelectedText = "\n";
        }
    }
}
